package elsa.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import elsa.bean.ScsArchives;
import elsa.bean.ScsOpsinsp;
import elsa.bean.service.*;
import elsa.bootmapper.OpsMapper;
import elsa.shipmapper.ServiceMapper;
import elsa.tools.Message;
import elsa.tools.MyResp;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@Controller
public class ServiceController {

    @Autowired
    private OpsMapper opsMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    //档案查询 兼 搜索  兼 分页
    @CrossOrigin
    @RequestMapping(value="/archives/ops", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public MyResp archivesOpsList(@RequestParam("pageIndex") Integer pageIndex,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam(value="value",required = false,defaultValue = "null") String value,
                                   @RequestParam(value="type",required = false,defaultValue = "null") String type){
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        int count = 0;
        List<ScsOpsinsp> list;
        PageHelper.startPage(pageIndex, pageSize);
        System.out.println(type);
        switch (type){
            case "rgstnum":
                PageHelper.startPage(pageIndex, pageSize);
                list = opsMapper.opsArchivesByRgstnum(value);
                count = opsMapper.opsArchivesCountByRgstnum(value);
                break;
            case "inspnum":
                PageHelper.startPage(pageIndex, pageSize);
                list = opsMapper.opsArchivesByInspnum(value);
                count = opsMapper.opsArchivesCountByInspnum(value);
                break;
            case "shipname":
                PageHelper.startPage(pageIndex, pageSize);
                list = opsMapper.opsArchivesByShipname(value);
                count = opsMapper.opsArchivesCountByShipname(value);
                break;
            case "":
                PageHelper.startPage(pageIndex, pageSize);
                list = opsMapper.opsArchives();
                count = opsMapper.opsArchivesCount();
                break;
            default:
                PageHelper.startPage(pageIndex, pageSize);
                list = opsMapper.opsArchives();
                count = opsMapper.opsArchivesCount();
        }
        JSONArray ja = JSONArray.parseArray(JSON.toJSONString(list));
        for (Object aJa : ja) {
            JSONObject job = (JSONObject) aJa;
            ScsArchives scsArchives = serviceMapper.findCbjbxx(job.get("inspnum").toString());
            if (scsArchives != null) {
                if (scsArchives.getZWCM() != null) {
                    job.put("shipname", scsArchives.getZWCM());
                } else {
                    job.put("shipname", "");
                }
                if (scsArchives.getZDW() != null) {
                    job.put("grosston", scsArchives.getZDW());
                } else {
                    job.put("grosston", "");
                }
                if (scsArchives.getCBZC() != null) {
                    job.put("shiplength", scsArchives.getCBZC());
                } else {
                    job.put("shiplength", "");
                }
                if (scsArchives.getZJZGL() != null) {
                    job.put("hostpower", scsArchives.getZJZGL());
                } else {
                    job.put("hostpower", "");
                }
            } else {
                job.put("shipname", "");
                job.put("grosston", "");
                job.put("shiplength", "");
                job.put("hostpower", "");
            }
        }
        ;
        String jsonStrng = JSON.toJSONString(ja);
        List<ScsOpsinsp> scsOpsinspList = JSON.parseArray(jsonStrng,ScsOpsinsp.class);
        return MyResp.render(0,"获取成功",scsOpsinspList,count);
    }

    //查询船舶基本信息存在（船检登记号/船名）
    @CrossOrigin
    @RequestMapping(value="/archives", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Message cbjbxxCheck(@RequestParam("type") String type,
                               @RequestParam("value") String value){
        String b = "";
        String c = "";
        switch (type){
            case "cjdjh":
                if(serviceMapper.findCbjbxx(value)!=null){
                    b = "获取成功!";
                    c = value;
                }else {
                    b = "船检登记号不存在!";
                }
                break;
            case "zwcm":
                try{
                    b = "获取成功!";
                    c = serviceMapper.findCbjbxxByZwcm(value).getCJDJH();
                }catch (NullPointerException e){
                    b = "船名不存在!";
                }catch (MyBatisSystemException e){
                    b = "船名有重复!";
                }
//                if(serviceMapper.findCbjbxxByZwcm(value)!=null){
//                    b = "获取成功!";
//                    c = serviceMapper.findCbjbxxByZwcm(value).getCJDJH();
//                }else{
//                    b = "船名";
//                }
                break;
            default:
                break;
        }
        return new Message(b,c);
    }

    //查询船舶基本信息（船检登记号）
    @CrossOrigin
    @RequestMapping(value="/archives/cbjbxx/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Cbjbxx cbjbxxInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        System.out.println(cjdjh);
        return serviceMapper.findCbjbxxByCjdjh(cjdjh);
    }

    //船舶起重设备证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zsqz/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zsqz zsqzInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZsqzByCjdjh(cjdjh);
    }

    //海上船舶乘客定额证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshck/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshck zshckInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshckByCjdjh(cjdjh);
    }

    //海上船舶船员舱室设备证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshcs/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshcs zshcsInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshcsByCjdjh(cjdjh);
    }

    //海上船舶防止散装运输有毒物质污染证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshfd/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshfd zshfdInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshfdByCjdjh(cjdjh);
    }

    //海上船舶防止空气污染证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshkq/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshkq zshkqInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshkqByCjdjh(cjdjh);
    }

    //海上船舶防污底系统证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshwd/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshwd zshwdInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshwdByCjdjh(cjdjh);
    }

    //海上船舶吨位证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshdw/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshdw ZshdwInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshdwByCjdjh(cjdjh);
    }

    //海上船舶防止生活污水污染证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshfs/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshfs zshfsInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshfsByCjdjh(cjdjh);
    }

    //海上船舶防止油污证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshyw/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshyw zshywInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshywByCjdjh(cjdjh);
    }

    //海上船舶免除证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshmc/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshmc zshmcInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshmcByCjdjh(cjdjh);
    }

    //海上船舶散装运输危险化学品适装证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshsz/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshsz zshszInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshszByCjdjh(cjdjh);
    }

    //海上船舶散装运输液化气体适装证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshsy/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshsy zshsyInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshsyByCjdjh(cjdjh);
    }

    //海上船舶载重线证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshzz/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshzz zshzzInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshzzByCjdjh(cjdjh);
    }

    //海上浮船坞安全证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshfc/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshfc zshfcInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshfcByCjdjh(cjdjh);
    }

    //海上高速船安全证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshgs/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshgs zshgsInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshgsByCjdjh(cjdjh);
    }

    //海上货船适航证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshhc/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshhc zshhcInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshhcByCjdjh(cjdjh);
    }

    //海上客船适航证书（检验编号）
    @CrossOrigin
    @RequestMapping(value="/archives/zshkc/{cjdjh}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public Zshkc zshkcInfo(@PathVariable("cjdjh") String cjdjh){
        if(cjdjh==null){
            cjdjh="";
        }
        return serviceMapper.findZshkcByCjdjh(cjdjh);
    }
}
